//
//  UIWindow+AG.swift
//  ASW
//
//  Created by zhuxuhong on 2020/12/23.
//

import UIKit
import BWToolKit

extension UIWindow {
    public func setRoot(
        viewController: UIViewController,
        duration: TimeInterval = 0.25,
        options: UIView.AnimationOptions = [
            .transitionCrossDissolve,
            .curveEaseInOut
        ],
        animated: Bool = true,
        completion: BlockT<Bool>? = nil
    ) {
        func _setRoot() {
            rootViewController = viewController
            makeKeyAndVisible()
        }
        guard animated else {
            _setRoot()
            return
        }
        viewController.modalTransitionStyle = .crossDissolve
        
        UIView.transition(
            with: self,
            duration: duration,
            options: options,
            animations: {
                let oldState = UIView.areAnimationsEnabled
                UIView.setAnimationsEnabled(false)
                _setRoot()
                UIView.setAnimationsEnabled(oldState)
            },
            completion: completion
        )
    }
}
